package com.probos.bosnatuur.monitor;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ProjectEdit extends Activity {

    private EditText mTitleText;
    private EditText mBodyText;
    private Long mRowId;
    private ProjectDbAdapter mDbHelper;

    public class MyOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext(), "The planet is " +
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        }
        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    }
    
    private void saveState() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();

        if (mRowId == null) {
            long id = mDbHelper.createProject(title, body);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateProject(mRowId, title, body);
        }
    }   
    
    private void populateFields() {
        if (mRowId != null) {
            Cursor note = mDbHelper.fetchProject(mRowId);
            startManagingCursor(note);
            mTitleText.setText(note.getString(note.getColumnIndexOrThrow(ProjectDbAdapter.KEY_TITLE)));
            mBodyText.setText(note.getString(note.getColumnIndexOrThrow(ProjectDbAdapter.KEY_DESCRIPTION)));
        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(ProjectDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // saveState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mDbHelper = new ProjectDbAdapter(this);
        mDbHelper.open();
        
        setContentView(R.layout.project_edit);
        setTitle(R.string.project_edit);

        mTitleText = (EditText) findViewById(R.id.project_title);
        mBodyText = (EditText) findViewById(R.id.project_text);

        Button confirmButton = (Button) findViewById(R.id.confirm);
        Button cancelButton = (Button) findViewById(R.id.cancel);

        mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState.getSerializable(ProjectDbAdapter.KEY_ROWID);
        if (mRowId == null) {
        	Bundle extras = getIntent().getExtras();
        	mRowId = (extras != null) ? extras.getLong(ProjectDbAdapter.KEY_ROWID) : null;
        }
        
        populateFields();

        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	saveState();
            	setResult(RESULT_OK);
                finish();
            }
        });
        
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	setResult(RESULT_CANCELED);
                finish();
            }
        });
    }
    
    @Override     
    protected void onDestroy() {
        mDbHelper.close();
        super.onDestroy();
    }
}
